Allow a key with credential instead of a key_file

Andrew Cantino 8 years ago
parent
commit
67fa70553d

+ 2 - 2
app/models/agents/google_calendar_publish_agent.rb

@@ -31,7 +31,7 @@ module Agents
31 31
 
32 32
       `google` `service_account_email` - The authorised service account.
33 33
 
34
-      `google` `key_file` - The path to the key file.
34
+      `google` `key_file` OR `google` `key` - The path to the key file or the key itself.  Liquid formatting is supported if you want to use a Credential.  (E.g., `{% credential google_key %}`)
35 35
 
36 36
       `google` `key_secret` - The secret for the key, typically 'notasecret'
37 37
 
@@ -91,7 +91,7 @@ module Agents
91 91
 
92 92
     def receive(incoming_events)
93 93
      incoming_events.each do |event|
94
-        calendar = GoogleCalendar.new(options, Rails.logger)
94
+        calendar = GoogleCalendar.new(interpolate_options(options, event), Rails.logger)
95 95
 
96 96
         calendar_event = JSON.parse(calendar.publish_as(interpolated(event)['calendar_id'], event.payload["message"]).response.body)
97 97
 

+ 8 - 2
lib/google_calendar.rb

@@ -1,7 +1,13 @@
1 1
 class GoogleCalendar
2 2
   def initialize(config, logger)
3 3
     @config = config
4
-    @key = Google::APIClient::PKCS12.load_key(@config['google']['key_file'], @config['google']['key_secret'])
4
+
5
+    if @config['google']['key'].present?
6
+      @key = OpenSSL::PKCS12.new(@config['google']['key'], @config['google']['key_secret']).key
7
+    else
8
+      @key = Google::APIClient::PKCS12.load_key(@config['google']['key_file'], @config['google']['key_secret'])
9
+    end
10
+
5 11
     @client = Google::APIClient.new(application_name: "Huginn", application_version: "0.0.1")
6 12
     @client.retries = 2
7 13
     @logger ||= logger
@@ -60,4 +66,4 @@ class GoogleCalendar
60 66
     @logger.debug ret.to_yaml
61 67
     ret    
62 68
   end
63
-end
69
+end

+ 22 - 2
spec/models/agents/google_calendar_publish_agent_spec.rb

@@ -70,14 +70,16 @@ describe Agents::GoogleCalendarPublishAgent, :vcr do
70 70
       }.to_json
71 71
     end
72 72
 
73
-    before do
73
+    def setup_mock!
74 74
       fake_interface = Object.new
75
-      mock(GoogleCalendar).new(agent.options, Rails.logger) { fake_interface }
75
+      mock(GoogleCalendar).new(agent.interpolate_options(agent.options), Rails.logger) { fake_interface }
76 76
       mock(fake_interface).publish_as(calendar_id, message) { stub!.response.stub!.body { response_body } }
77 77
     end
78 78
 
79 79
     describe 'when the calendar_id is in the options' do
80 80
       it 'should publish any payload it receives' do
81
+        setup_mock!
82
+
81 83
         expect {
82 84
           agent.receive([event])
83 85
         }.to change { agent.events.count }.by(1)
@@ -88,6 +90,8 @@ describe Agents::GoogleCalendarPublishAgent, :vcr do
88 90
 
89 91
     describe 'with Liquid templating' do
90 92
       it 'should allow Liquid in the calendar_id' do
93
+        setup_mock!
94
+
91 95
         agent.options['calendar_id'] = '{{ cal_id }}'
92 96
         agent.save!
93 97
 
@@ -99,6 +103,22 @@ describe Agents::GoogleCalendarPublishAgent, :vcr do
99 103
         expect(agent.events.count).to eq(1)
100 104
         expect(agent.events.last.payload).to eq({ "success" => true, "published_calendar_event" => JSON.parse(response_body), "agent_id" => event.agent_id, "event_id" => event.id })
101 105
       end
106
+
107
+      it 'should allow Liquid in the key' do
108
+        agent.options['google'].delete('key_file')
109
+        agent.options['google']['key'] = '{% credential google_key %}'
110
+        agent.save!
111
+
112
+        users(:jane).user_credentials.create! credential_name: 'google_key', credential_value: 'something'
113
+
114
+        agent.reload
115
+
116
+        setup_mock!
117
+
118
+        agent.receive([event])
119
+
120
+        expect(agent.events.count).to eq(1)
121
+      end
102 122
     end
103 123
   end
104 124
 end